package com.ash.service.order;

import com.ash.domain.Books;
import com.ash.domain.Order;
import com.ash.domain.vo.ChartVo;
import com.ash.domain.vo.OrderDetailVo;
import com.ash.mapper.book.BooksMapper;
import com.ash.mapper.order.OrderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/4/13 17:49
 * @file : project06.com.ash.service.order.OrderServiceImpl.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;
    @Resource
    BooksMapper booksMapper;
    @Override
    public Integer add(Order order) {
        return orderMapper.add(order);
    }
    @Override
    public List<Order> selectByUid(Integer uid, Integer page, Integer size) {
        if(page!=null&&size!=null){
            return orderMapper.selectByUid(uid,(page-1)*size,size);
        }
        return orderMapper.selectByUid(uid,null,null);
    }

    @Override
    public Integer dataCount(Integer uid) {
        return orderMapper.dataCount(uid);
    }

    @Override
    public Integer addDetail(List<ChartVo> chartVos, Integer oid) {
        /*更新书籍数量*/
        for (ChartVo chartVo : chartVos) {
            Books books = booksMapper.selectById(chartVo.getBid());
            booksMapper.setCountById(chartVo.getBid(),books.getCount()-chartVo.getCount());
        }
        return orderMapper.addDetail(chartVos,oid);
    }

    @Override
    public List<OrderDetailVo> selectDetailByOid(Integer oid) {
        return orderMapper.selectDetailByOid(oid);
    }
}
